import ElementUI from 'element-ui'
import Vue from 'vue'
import ConfirmMessage from './ConfirmMessage.vue'
let mountNode
export class Communication {
  constructor(resolve, reject) {
    this.resolve = resolve
    this.reject = reject
  }
  callbackFn(fn) {
    fn.then(() => {
      console.log('确认')
      this.resolve()
    })
      .catch(() => {
        console.log('取消')
        this.reject()
      })
      .finally(removeNode)
  }
}
const addNode = (keys) => {
  mountNode = document.createElement('div')
  mountNode.setAttribute('id', 'box')
  document.body.appendChild(mountNode)
  Vue.use(ElementUI)
  new Vue({
    render: (h) => h(ConfirmMessage, { props: { keys } })
  }).$mount('#box')
}
const removeNode = () => {
  const node = document.getElementById('dialog-message')
  document.body.removeChild(node)
}
function confirmMessage(options = {}) {
  return new Promise((resolve, reject) => {
    const communicate = new Communication(resolve, reject)
    const keys = Object.assign(options, { communicate })
    addNode(keys)
  })
}
export default confirmMessage
